//
//  YAYIPerfectMessageCell.m
//  yayi
//
//  Created by veer on 16/7/12.
//  Copyright © 2016年 veer. All rights reserved.
//

#import "YAYIPerfectMessageCell.h"

#import "YAYIMessageListModel.h"

#import "NSDate+VR.h"


@implementation YAYIPerfectMessageCell

-(void)setModel:(YAYIMessageListModel *)model
{
    _model = model;
    
    self.titleView.text = model.title;
    self.content.text = model.brief;
    self.timeLabel.text = [YAYIMessageListModel timeString:_model.createtime];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.createtime / 1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-MM-dd";
    
    if ([date isToday]) {
        formatter.dateFormat = @"HH:mm";
    }
    
    self.timeLabel.text = [formatter stringFromDate:date];
    CGSize size = [self.timeLabel.text sizeWithFont:self.timeLabel.font];
    [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(size.width + 12);
    }];
    
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",API,_model.picurl]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
}


- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    self.bgView.backgroundColor = highlighted ? YAYIColor(0xee, 0xee, 0xee) : [UIColor whiteColor];
}


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"systemMessageCell";
    
    YAYIPerfectMessageCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIPerfectMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        [self setupSubView];
    }
    return self;
}


-(void)setupSubView{
    
    self.timeLabel = [UILabel new];
    self.timeLabel.text = @"20:30";
    self.timeLabel.layer.masksToBounds = YES;
    self.timeLabel.layer.cornerRadius = 5;
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.font = [UIFont systemFontOfSize:font13];
    self.timeLabel.backgroundColor = [UIColor lightGrayColor];
    self.timeLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:self.timeLabel];
    
    self.bgView = [UIView new];
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 5;
//    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    
    self.titleView = [UILabel new];
    self.titleView.numberOfLines = 0;
    self.titleView.text = @"在线医生";
    self.titleView.font = [UIFont systemFontOfSize:15];
    self.titleView.textColor = [UIColor colorWithRed:67/255.0 green:66/255.0 blue:66/255.0 alpha:1];
    [self.bgView addSubview:self.titleView];
    
    self.picImageView = [UIImageView new];
    self.picImageView.image = [UIImage imageNamed:@"systeminfo"];
    [self.picImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    self.picImageView.contentMode = UIViewContentModeScaleToFill;
    self.picImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.picImageView.clipsToBounds  = YES;
    self.picImageView.layer.cornerRadius = 5;
    self.picImageView.layer.masksToBounds = YES;
    [self.bgView addSubview:self.picImageView];
    
    self.content = [UILabel new];
    self.content.font = [UIFont systemFontOfSize:14];
    self.content.numberOfLines = 0;
    self.content.text = @"琼库什台草原位于新疆伊犁特克斯县南部山区，与喀拉峻草原隔河相望。琼库什台，在哈萨克语中是“大平台子”的意思。这个天山脚下的美丽草原，大台地上绿草如茵，纵切出无数的沟壑";
    self.content.textColor = [UIColor colorWithRed:87/255.0 green:85/255.0 blue:85/255.0 alpha:1];
    [self.bgView addSubview:self.content];
    
    self.separeLine = [UILabel new];
    self.separeLine.backgroundColor = [UIColor lightGrayColor];
    self.separeLine.alpha = 0.5;
    [self.bgView addSubview:self.separeLine];
    
    self.subtitle = [UILabel new];
    self.subtitle.text = @"阅读全文";
//    self.subtitle.textColor = [UIColor colorWithRed:67/255.0 green:66/255.0 blue:66/255.0 alpha:1];
    self.subtitle.font = [UIFont systemFontOfSize:font14];
    [self.bgView addSubview:self.subtitle];
    
    self.icon = [UIImageView new];
    self.icon.image = [UIImage imageNamed:@"Icon_1"];
    [self.bgView addSubview:self.icon];

}


-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@(15));
        make.centerX.mas_equalTo(self.contentView);
        //        make.size.mas_equalTo(CGSizeMake(50, 18));
        make.width.mas_greaterThanOrEqualTo(50);
        make.height.mas_equalTo(18);
    }];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgView.mas_left).offset(12);
        make.top.mas_equalTo(self.bgView.mas_top).with.offset(15);
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(-15);
    }];
    
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgView.mas_left).offset(12);
        make.top.mas_equalTo(self.titleView.mas_bottom).with.offset(15);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width - 34 * 2, 278 * YAYIScreenScale));
    }];
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-38 - 15);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(15);
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(-15);
    }];
    
    [self.separeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-38);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(15);
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(-15);
        make.height.equalTo(@0.5);
    }];
    
    [self.subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(15);
        make.height.mas_equalTo(38);
    }];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.subtitle);
        make.right.mas_equalTo(self.bgView.mas_right).offset(-15);
        make.size.mas_equalTo(CGSizeMake(7, 12));
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(@22);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-22);
        make.bottom.mas_equalTo(self.contentView).offset(0);
    }];
    
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(15, 20, SCREEN_width - 30, 300)];
    self.selectedBackgroundView.backgroundColor = [UIColor redColor];
}


-(CGFloat)heightForRow:(NSString *)str font:(UIFont *)font width:(CGFloat)width
{
    CGSize size = [str sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    return size.height;
}

@end
